package com.briup.jd2407cms.web.controller;

import com.briup.jd2407cms.aop.Logging;
import com.briup.jd2407cms.domain.Category;
import com.briup.jd2407cms.service.CategoryService;
import com.briup.jd2407cms.util.result.Result;
import com.briup.jd2407cms.web.listener.CategoryListener;
import com.briup.jd2407cms.util.excel.CategoryParentIdConverter;
import com.briup.jd2407cms.util.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: JD2407-cms
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-18  16:27
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "栏目模块")
@RestController
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "查询所有1级栏目(含2级)", notes = "不需要分页")
    @Logging("查询所有1级栏目(含2级)")
    @GetMapping("/queryAllParent")
    public Result queryAllParent() {
        return Result.success(categoryService.selectAllParent());
    }

    @ApiOperation(value = "新增栏目", notes = "栏目名必须唯一，如果为二级栏目则其父栏目id必须有效")
    @Logging("新增栏目")
    @PostMapping("/save")
    public Result save(@RequestBody Category category) {
        return Result.success(categoryService.save(category) ? "操作成功" : "操作失败");
    }

    @ApiOperation(value = "根据id查询栏目信息", notes = "该接口用于在修改栏目前的数据回显")
    @Logging("根据id查询栏目信息")
    @GetMapping("/queryById/{id}")
    public Result getCategoryById(@PathVariable String id) {
        return Result.success(categoryService.getById(id));
    }

    @ApiOperation(value = "更新栏目", notes = "栏目名必须唯一，栏目级别不能改动")
    @Logging("更新栏目")
    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        return Result.success(categoryService.updateById(category) ? "操作成功" : "操作失败");
    }

    @ApiOperation(value = "根据id删除栏目", notes = "id必须存在且有效")
    @Logging("根据id删除栏目")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        return Result.success("删除成功");
    }

    @ApiOperation(value = "批量删除栏目", notes = "需要提供一个或多个id值")
    @Logging("批量删除栏目")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteCategoryInBatch(@PathVariable("ids") List<Integer> ids) {
        return Result.success(categoryService.removeByIds(ids)?"删除成功":"删除失败");
    }

    @ApiOperation(value = "分页查询所有栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int",
                    required = true, defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int",
                    required = true, defaultValue = "4", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父栏目id",
                    dataType = "int", paramType = "query")})
    @Logging("分页查询所有栏目")
    @GetMapping("/query")
    public Result query(Integer pageNum, Integer pageSize, Integer parentId) {
        return Result.success(categoryService.page(pageNum, pageSize, parentId));
    }

    @ApiOperation("导入Excel栏目数据")
    @Logging("导入栏目数据")
    @PostMapping( "/import")
    public Result imports(@RequestPart MultipartFile file) {
        //导入前,更新一级栏目集合,防止在导入前数据库中的一级栏目被更新
        CategoryParentIdConverter.list = categoryService.queryAllOneLevel();
        //获取数据
        List<Category> list = excelUtils.importData(file, Category.class, new CategoryListener());
        //导入数据到数据库中
        categoryService.insertBatch(list);

        return Result.success("Excel数据导入成功");
    }

    // 下载文件不需要返回统一的结果响应
    // 需要自己包装response
    @ApiOperation("导出栏目数据")
//    @Logging("导出栏目数据")
    @GetMapping(value = "/export", produces = "application/octet-stream")
    public Result exports(HttpServletResponse response) {
        // 1、导出前,更新一级栏目集合,防止在导出前数据库中的一级栏目被更新
        // 获取所有的父级栏目，保证数据库里是最新的
        CategoryParentIdConverter.list = categoryService.queryAllOneLevel();
        // 2、获取栏目数据
//        List<Category> list = categoryService.queryAllOneLevel();
//        List<CategoryExtend> list = categoryService.selectAllWithChild();
        List<Category> list = categoryService.getAll();

        // 3、导出数据，提供下载
        excelUtils.exportExcel(response, list, Category.class, "栏目信息表");

        return Result.success("数据导出成功");
    }

}
